﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Radovici.Ctrls.Desktop;
using Radovici.SharedLibrary;

namespace Radovici.Ctrls.ContentControls
{
    public class Modal : Dragable
    {
        #region Private fields
        private Rectangle _modalBackground;
        #endregion

        public Modal()
        {
            Canvas root = Canvas;
            if (root != null)
            {
                Dispatcher.BeginInvoke(
                    delegate
                        {
                            lock (root)
                            {
                                _modalBackground = root.Children.SingleOrDefault(ele => ele is Rectangle && (string.Equals(((Rectangle)ele).Tag, "modal"))) as Rectangle;
                                if (_modalBackground == null)
                                {
                                    _modalBackground = new Rectangle() {Tag = "modal", Fill = new SolidColorBrush(Color.FromArgb(50, 0, 0, 0))};
                                    _modalBackground.Height = root.ActualHeight;
                                    _modalBackground.Width = root.ActualWidth;
                                    root.SizeChanged += delegate(object sender, SizeChangedEventArgs sizeEventArgs)
                                                            {
                                                                _modalBackground.Height = sizeEventArgs.NewSize.Height;
                                                                _modalBackground.Width = sizeEventArgs.NewSize.Width;
                                                            };
                                    root.Children.Add(_modalBackground);
                                    Canvas.SetZIndex(_modalBackground, TopMostZIndex);                                    
                                }                                
                                //Binding attempt #1
                                //Binding bHeight = new Binding("ActualHeight") { Source = Application.Current.RootVisual };
                                //Binding bWidth = new Binding("ActualWidth") { Source = Application.Current.RootVisual };
                                //modalCover.SetBinding(HeightProperty, bHeight);
                                //modalCover.SetBinding(WidthProperty, bWidth);
                                //Binding attempt #2
                                //BindingHelper.SetBinding(Application.Current.RootVisual as FrameworkElement, ActualWidthProperty, modalCover, WidthProperty);
                                //BindingHelper.SetBinding(Application.Current.RootVisual as FrameworkElement, ActualHeightProperty, modalCover, HeightProperty);
                            }
                        }
                    );
            }
            IsDragPermanent = true;
        }

        public event CancelEventHandler Closed;

        private UIElement _anchor;

        public void Show()
        {
            Show(null);
        }

        public void Show(UIElement anchor)
        {
            _anchor = anchor;
            Canvas root = Canvas;
            if (root != null)
            {
                Dispatcher.BeginInvoke(delegate
                {
                    root.Children.Add(this);
                });
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Dispatcher.BeginInvoke(delegate{ Popout(); });
            var ctrl = this.Content as UIElement;
            if (ctrl != null)
            {
                ctrl.KeyUp += new KeyEventHandler(OnContentKeyUp);
            }
            var widget = this.Content as Widget;
            if (widget != null)
            {
                widget.Closed += delegate(object sender, CancelEventArgs e) { Close(e); };
            }
        }        

        private void Close()
        {
            Close(new CancelEventArgs(false));
        }

        private void Close(CancelEventArgs e)
        {
            Canvas root = Canvas;
            if (root != null)
            {
                Dispatcher.BeginInvoke(delegate
                                           {
                                               Popin();
                                               if (root.Children.Contains(this))
                                               {
                                                   root.Children.Remove(this);
                                               }
                                               if (root.Children.Contains(_modalBackground))
                                               {
                                                   root.Children.Remove(_modalBackground);
                                               }
                                               if (Closed != null)
                                               {
                                                   Closed(this.Content, e);
                                               }
                                           });
            }            
        }

        private void OnContentKeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Escape:
                    this.Close(new CancelEventArgs(true));
                    break;
            }
        }
    }
}
